package com.zengdada.dml.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zengdada.authority.mapper.DmlColumnMapper;
import com.zengdada.authority.mapper.DmlMapper;
import com.zengdada.authority.model.Dml;
import com.zengdada.authority.model.DmlColumn;
import com.zengdada.dml.model.DmlExtend;
import com.zengdada.dml.model.DmlColumnExtend;
import com.zengdada.dml.model.type.*;
import com.zengdada.dml.model.type.StaticSelect;
import com.zengdada.dml.utils.TaggedSql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.Fn;
import tk.mybatis.mapper.weekend.WeekendSqls;
import tk.mybatis.mapper.weekend.reflection.Reflections;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Service
public class DmlPlatService {


    @Autowired
    DmlMapper dmlMapper;

    @Autowired
    DmlColumnMapper dmlColumnMapper;

    public DmlExtend loadDml(String dmlId) throws ParseException {
        Dml dml = dmlMapper.selectByPrimaryKey(dmlId);
        if (dml == null) return null;
        Class<? extends DmlExtend> dmlClass;
        DmlExtend dmlExtend;
        switch (dml.getDmlType()) {
            case DmlExtend.DML_TYPE_ANY_WHERE:
            case DmlExtend.DML_TYPE_STATIC_SELECT:
                dmlClass = StaticSelect.class;
                break;
            case DmlExtend.DML_TYPE_INSERT:
                dmlClass = Insert.class;
                break;
            case DmlExtend.DML_TYPE_DELETE:
                dmlClass = Delete.class;
                break;
            case DmlExtend.DML_TYPE_PLSQL:
                dmlClass = PlSql.class;
                break;
            case DmlExtend.DML_TYPE_UPDATE:
                dmlClass = Update.class;
                break;
            case DmlExtend.DML_TYPE_FUNC:
                dmlClass = Function.class;
                break;
            case DmlExtend.DML_TYPE_PROC:
                dmlClass = Procedure.class;
                break;
            case DmlExtend.DML_TYPE_DYNAMIC_SELECT:
                throw new IllegalArgumentException("没有实现");
            default:
                throw new IllegalArgumentException(String.format("语句类型不受支持:%d", dml.getDmlType()));
        }
        dmlExtend = JSONObject.parseObject(JSONObject.toJSONString(dml), dmlClass);
        dmlExtend.setVersion(dml.getModifyTime().getTime());

//        List<DmlColumnExtend> columns = new ArrayList<>();
        List<DmlColumn> dmlColumns = dmlColumnMapper.selectByExample(new Example.Builder(DmlColumn.class)
                .where(WeekendSqls.<DmlColumn>custom().andEqualTo(DmlColumn::getDmlId, dmlId).andEqualTo(DmlColumn::getIsValid, 1))
                .orderByAsc(Reflections.fnToFieldName((Fn<DmlColumn, Object>) DmlColumn::getColumnIndex)).build());
        List<DmlColumnExtend> columns = JSONObject.parseArray(JSONObject.toJSONString(dmlColumns), DmlColumnExtend.class);
        if (columns == null) columns = new ArrayList<>();
        //        dmlColumns.forEach(column -> {
//            DmlColumnExtend columnExtend = JSONObject.parseObject(JSONObject.toJSONString(column), DmlColumnExtend.class);
//            String extype1 = column.getExtype1();
//            if (extype1 != null) {
//                if (!SQL_MAP.containsKey(extype1))
//                    throw new IllegalArgumentException(String.format("SQL类型不受支持：%s", extype1));
//                columnExtend.setSqlType(SQL_MAP.get(extype1));
//            }
//            try {
//                columnExtend.setDataTypeClass(Class.forName(column.getDataType()));
//            } catch (ClassNotFoundException e) {
//                e.printStackTrace();
//            }
//            columns.add(columnExtend);
//        });


        // 如果没有定义参数列，则尝试从语句中解析 -- 不判断
        TaggedSql taggedSql = TaggedSql.compile(dmlExtend.getRawDml(), columns);
        dmlExtend.setRawDml(taggedSql.getText());
        dmlExtend.addAll(columns);
        return dmlExtend;
    }

}
